package com.compass.mobile.ui.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.leanback.widget.ArrayObjectAdapter
import androidx.leanback.widget.BaseGridView
import androidx.leanback.widget.FocusHighlight
import androidx.leanback.widget.FocusHighlightHelper
import com.compass.mobile.R
import com.compass.mobile.callback.OnItemClickListener
import com.compass.mobile.databinding.FragmentFollowingPrescriptionBinding
import com.compass.mobile.framework.base.BaseVBFragment
import com.compass.mobile.framework.ext.gone
import com.compass.mobile.framework.ext.visible
import com.compass.mobile.framework.utils.dpToPx
import com.compass.mobile.models.NotificationBean
import com.compass.mobile.presenters.NotificationPresenter
import com.compass.mobile.ui.dialog.CommonMessageDialog
import com.compass.mobile.ui.viewModel.UserViewModel
import com.compass.mobile.widget.MyBridgeAdapter
import getStringFromResource

/**
 * Description： 消息通知
 * Date:2024/6/8 18:25
 */
class NotificationFragment : BaseVBFragment<FragmentFollowingPrescriptionBinding>() {

    private var messageList: MutableList<NotificationBean> = mutableListOf()//包装集合
    private val userViewModel: UserViewModel by viewModels()
    private lateinit var arrayObjectAdapter: ArrayObjectAdapter
    override fun initView(view: View, savedInstanceState: Bundle?) {
        specialColumn()
    }

    private var mPage = 1
    override fun initData() {
        showLoading()
        var userId = UserInfoManager.getPatientInfo()?.id
        userViewModel.geMessage(userId, mPage)
        userViewModel.messageListLiveData.observe(this) {
            it?.let { list ->
                messageList = list
                if (messageList.isNotEmpty()) {
                    mBinding?.emptyView?.gone()
                    arrayObjectAdapter.clear()
                    arrayObjectAdapter.addAll(0, messageList)
                    mBinding?.caseItem?.requestFocus()
                } else {
                    mBinding?.emptyView?.visible()
                }
            } ?: kotlin.run {
                mBinding?.emptyView?.visible()
            }
            dismissLoading()
        }
    }

    @SuppressLint("RestrictedApi")
    fun specialColumn() {
        mBinding?.caseItem?.setNumColumns(1)
        mBinding?.caseItem?.verticalSpacing = dpToPx(10)
        arrayObjectAdapter = ArrayObjectAdapter(NotificationPresenter())
        val itemBridgeAdapter = MyBridgeAdapter(
            requireContext(),
            arrayObjectAdapter, null
        )
        mBinding?.caseItem?.setAdapter(itemBridgeAdapter)
        mBinding?.caseItem?.setFocusScrollStrategy(BaseGridView.FOCUS_SCROLL_ITEM)
        arrayObjectAdapter.addAll(0, messageList)
        FocusHighlightHelper.setupBrowseItemFocusHighlight(
            itemBridgeAdapter,
            FocusHighlight.ZOOM_FACTOR_NONE, false
        )


    }

}